package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic("server/vms/completed")
public class CompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        System.err.println("接收到的消息是：" + jsonMsg);
        TaskCompleteContract completeContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        // 判断结果是否为空
        if(completeContract==null || Strings.isNullOrEmpty(completeContract.getInnerCode())  ) return;
        String innerCode = completeContract.getInnerCode();
        int taskType = completeContract.getTaskType();
        // 判断工单类型
        // 是投放工单
        if ( taskType == VMSystem.TASK_TYPE_DEPLOY){
            vendingMachineService.updateStatus(innerCode,VMSystem.VM_STATUS_RUNNING);
        }

        //如果是撤机工单，将售货机修改为撤机状态
        if( taskType== VMSystem.TASK_TYPE_REVOKE){
            vendingMachineService.updateStatus(  innerCode, VMSystem.VM_STATUS_REVOKE  );
        }
    }
}